package com.sunricher.telinkblemesh.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunricher.telinkblemesh.R;
import com.sunricher.telinkblemeshlib.MeshCommand;
import com.sunricher.telinkblemeshlib.db.UartDaliDeviceManager;
import com.sunricher.telinkblemeshlib.models.UartDaliDevice;
import com.sunricher.telinkblemeshlib.models.UartDaliManager;

import java.util.HashMap;

public class DaliScenesActivity extends AppCompatActivity {

    private int gatewayAddress;

    private EditText sceneEditText;
    private Button activateButton;
    private EditText deviceEditText;
    private TextView sceneResultTextView;
    private Button getSceneButton;
    private EditText sceneValueEditText;
    private Button setSceneButton;
    private Button setSceneMaskButton;
    private TextView commandResultTextView;

    private UartDaliManager.Callback callback = new UartDaliManager.Callback() {
        @Override
        public void didExecuteCommandOK(int daliAddress, int gatewayAddress, int cmdType, int cmd) {
            String text = "OK, " + "daliAddress " + daliAddress + ", cmdType " + cmdType + ", cmd " + cmd;
            commandResultTextView.setText(text);
        }

        @Override
        public void didExecuteCommandFailed(int daliAddress, int gatewayAddress, int reason, int cmdType, int cmd) {
            String text = "Failed, " + "daliAddress " + daliAddress + ", cmdType " + cmdType + ", cmd " + cmd;
            commandResultTextView.setText(text);
        }

        @Override
        public void didGetDeviceSceneValue(HashMap<String, Object> sceneValue, int daliAddress, int gatewayAddress) {
            String text = "";
            if (sceneValue.containsKey("BRIGHTNESS")) {
                text = "Brightness " + sceneValue.get("BRIGHTNESS");
            }
            sceneResultTextView.setText(text);
        }

        @Override
        public void didUpdateDeviceSceneValue(int daliAddress, int gatewayAddress) {
            commandResultTextView.setText("did update device scene value");
        }
    };

    private int getSceneId() {
        int scene = 0;
        String newValue = sceneEditText.getText().toString();
        if (newValue != null && newValue.length() > 0) {
            scene = Integer.parseInt(newValue);
        }
        return scene;
    }

    public int getDeviceId() {
        int device = 0;
        String newValue = deviceEditText.getText().toString();
        if (newValue != null && newValue.length() > 0) {
            device = Integer.parseInt(newValue);
        }
        return device;
    }

    private String getDeviceType() {
        int deviceId = getDeviceId();
        UartDaliDevice device = UartDaliDeviceManager.getInstance(this).getDevice(deviceId, gatewayAddress);
        if (device != null) {
            return device.getDeviceType();
        }
        return UartDaliDevice.DEVICE_TYPE_DT6;
    }

    private HashMap<String, Object> getSceneValue() {
        HashMap<String, Object> value = new HashMap<>();
        String newValue = sceneValueEditText.getText().toString();
        if (newValue != null && newValue.length() > 0) {
           value.put("BRIGHTNESS", Integer.parseInt(newValue));
        }
        return value;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dali_scenes);

        gatewayAddress = getIntent().getIntExtra("gatewayAddress", 0);

        sceneEditText = findViewById(R.id.scene_et);
        activateButton = findViewById(R.id.activate_btn);
        deviceEditText = findViewById(R.id.device_et);
        sceneResultTextView = findViewById(R.id.scene_result_tv);
        getSceneButton = findViewById(R.id.get_scene_btn);
        sceneValueEditText = findViewById(R.id.scene_value_et);
        setSceneButton = findViewById(R.id.set_scene_btn);
        setSceneMaskButton = findViewById(R.id.set_scene_mask_btn);
        commandResultTextView = findViewById(R.id.command_result_tv);

        activateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().executeScene(gatewayAddress, getSceneId());
            }
        });

        getSceneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().getDeviceSceneValue(gatewayAddress, getDeviceId(), getSceneId());
            }
        });

        setSceneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UartDaliManager.getInstance().setDeviceSceneValue(gatewayAddress, getDeviceId(), getSceneId(), getSceneValue(), getDeviceType());
            }
        });

        setSceneMaskButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> maskValue = new HashMap<>();
                UartDaliManager.getInstance().setDeviceSceneValue(gatewayAddress, getDeviceId(), getSceneId(), maskValue, getDeviceType());
            }
        });

        UartDaliManager.getInstance().setCallback(callback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        UartDaliManager.getInstance().setCallback(null);
    }
}